package itsm.isperp.framework.exception;

import java.nio.charset.Charset;

import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.multipart.MaxUploadSizeExceededException;

/**
 * A class with "global" {@code @ExceptionHandler} methods for use across
 * controllers.
 * 
 * <p>
 * Enabled through {@link ExtendedExceptionHandlerExceptionResolver}. *
 * 
 * @author lizx
 * @date 2013-3-3
 * @version V1.0
 */
public class GlobalExceptionHandler {
	@ExceptionHandler(value = { MaxUploadSizeExceededException.class,
			BusinessException.class })
	public ResponseEntity<String> handle(Exception exception) {
		if (exception instanceof BusinessException) {
			BusinessException businessException = (BusinessException) exception;
			HttpHeaders headers = new HttpHeaders();
			MediaType mt = new MediaType("text", "html",
					Charset.forName("utf-8"));
			headers.setContentType(mt);
			return new ResponseEntity<String>("{success:false, msg:'"
					+ businessException.getMessage() + "'}", headers,
					HttpStatus.PRECONDITION_FAILED);// EXCEPTION_FAILURE
		}
		if (exception instanceof MaxUploadSizeExceededException) {

		}
		return null;
	}

}
